import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_view.dart';
import 'package:school_assistant_m/app/modules/mine/controllers/edit_password_controller.dart';

class EditPasswordView extends GetView<EditPasswordController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false, //输入框抵住键盘
      appBar: AppBar(
        elevation: 0.2,
        leading: IconButton(
          onPressed: Get.back,
          icon: Icon(
            Icons.arrow_back_ios,
            color: Colors.black,
          ),
        ),
        backgroundColor: Color(0xFFEDEDED),
        title: Text(
          '修改密码',
          style: TextStyle(
            color: Colors.black,
            fontWeight: FontWeight.w700,
          ),
        ),
        centerTitle: true,
        actions: [
          Padding(
            padding: EdgeInsets.fromLTRB(0.0, 11.0, 8.0, 11.0),
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8.0),
                ),
                primary: Colors.blue,
                elevation: 0.5,
              ),
              onPressed: () {
                controller.editPassword();
              },
              child: Text(
                '保存',
                style: TextStyle(
                  fontSize: 16,
                ),
              ),
            ),
          ),
        ],
      ),
      body: GetBuilder<EditPasswordController>(
          id: 'edit_password',
          init: controller,
          builder: (_) {
            return Container(
              margin: EdgeInsets.only(left: 20, right: 20),
              child: Form(
                key: controller.editPasswordKey,
                child: Column(
                  children: [
                    TextFormField(
                      autovalidateMode: AutovalidateMode.disabled,
                      controller: controller.username,
                      readOnly: true,
                      style: TextStyle(fontSize: 20.0),
                      decoration: InputDecoration(
                        labelText: '账号',
                        icon: Icon(Icons.person, color: Colors.blue),
                      ),
                    ),
                    TextFormField(
                      autovalidateMode: AutovalidateMode.disabled,
                      controller: controller.oldPassword,
                      style: TextStyle(fontSize: 20.0),
                      decoration: InputDecoration(
                        labelText: '原密码',
                        icon: Icon(
                          Icons.lock,
                          color: Colors.red,
                        ),
                        hintText: '请输入原密码',
                        suffixIcon: IconButton(
                          icon: Icon(controller.isShowOldPassword
                              ? Icons.visibility
                              : Icons.visibility_off),
                          onPressed: () {
                            controller.changeIsShowOldPassword();
                          },
                        ),
                      ),
                      obscureText: !controller.isShowOldPassword,
                      validator: (oldPassword) {
                        String res = controller.verifyOldPassword(oldPassword);
                        return res == '' ? null : res;
                      },
                    ),
                    TextFormField(
                      autovalidateMode: AutovalidateMode.disabled,
                      controller: controller.newPassword,
                      style: TextStyle(fontSize: 20.0),
                      decoration: InputDecoration(
                        labelText: '新密码',
                        icon: Icon(
                          Icons.lock,
                          color: Colors.green,
                        ),
                        hintText: '请输入新密码',
                        suffixIcon: IconButton(
                          icon: Icon(controller.isShowNewPassword
                              ? Icons.visibility
                              : Icons.visibility_off),
                          onPressed: () {
                            controller.changeIsShowNewPassword();
                          },
                        ),
                      ),
                      obscureText: !controller.isShowNewPassword,
                      validator: (newPassword) {
                        String res = controller.verifyNewPassword(newPassword);
                        return res == '' ? null : res;
                      },
                    ),
                    TextFormField(
                      autovalidateMode: AutovalidateMode.disabled,
                      controller: controller.newPasswordConfirm,
                      style: TextStyle(fontSize: 20.0),
                      decoration: InputDecoration(
                        labelText: '确认密码',
                        icon: Icon(
                          Icons.lock,
                          color: Colors.green,
                        ),
                        hintText: '请输入确认密码',
                        suffixIcon: IconButton(
                          icon: Icon(controller.isShowConfirmPassword
                              ? Icons.visibility
                              : Icons.visibility_off),
                          onPressed: () {
                            controller.changeIsShowConfirmPassword();
                          },
                        ),
                      ),
                      obscureText: !controller.isShowConfirmPassword,
                      validator: (confirmPassword) {
                        String res = controller.verifyConfirmPassword(confirmPassword);
                        return res == '' ? null : res;
                      },
                    ),
                  ],
                ),
              ),
            );
          }),
    );
  }
}
